#import "dfhgwhruebvhrheyavenuetyre.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheysavingmuscular.h"
#import "dfhgwhruebvhrheyinventorylatent.h"
#import <AliyunOSSiOS/AliyunOSSiOS.h>
static NSString *kTempFolder = @"CME_";
static NSString *const BucketName = @"cmateimg";
static NSString *const AliYunHost = @"http://oss-ap-southeast-1.aliyuncs.com";
@implementation dfhgwhruebvhrheysavingmuscular
+ (void)getAliYunConfigure:(void(^)(dfhgwhruebvhrheyinventorylatent *config))result {
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] ppp_fetchMethod:PPP_HttpMethodGet path:aliYunConfigApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		dfhgwhruebvhrheyinventorylatent *model = [dfhgwhruebvhrheyinventorylatent yy_modelWithJSON:json];
		if (model.code == 200) {
			if (result) {
				result(model);
			}
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		NSLog(@"==== %@", error);
	}];
}
+ (void)uploadImage:(UIImage *)image withType:(NSInteger)type andResult:(void(^)(BOOL completion, NSString *name, long long size))result {
	[self getAliYunConfigure:^(dfhgwhruebvhrheyinventorylatent *config) {
		id<OSSCredentialProvider> credential = [[OSSFederationCredentialProvider alloc] initWithFederationTokenGetter:^OSSFederationToken * _Nullable{
			OSSFederationToken *token = [[OSSFederationToken alloc] init];
			token.tToken = config.SecurityToken;
			token.tAccessKey = config.AccessKeyId;
			token.tSecretKey = config.AccessKeySecret;
			token.expirationTimeInGMTFormat = config.Expiration;
			return token;
		}];
		[self startLoaderImage:image byType:type withCredential:credential andResult:result];
	}];
}
+ (void)startLoaderImage:(UIImage *)sourceImg byType:(NSInteger)type withCredential:(id<OSSCredentialProvider>)credential andResult:(void(^)(BOOL completion, NSString *name, long long size))result {
	OSSClient *client = [[OSSClient alloc] initWithEndpoint:AliYunHost credentialProvider:credential];
	OSSPutObjectRequest *put = [[OSSPutObjectRequest alloc] init];
	put.bucketName = BucketName;
	NSString *url = [self generateStringWithBit:16].dhgfywegwygeydwscholarshiptexture;
	NSString *imageName = [NSString stringWithFormat:@"%@%@", kTempFolder, [url stringByAppendingString:@".png"]];
	put.objectKey = imageName;
	put.contentType = @"image/png";
	NSData *imageData = [self compressImage:sourceImg inKiloByte:300];
	put.uploadingData = imageData;
	OSSTask *putTask = [client putObject:put];
	if (type == 1) {
		[dfhgwhruebvhrheyavenuetyre addTracking:face_size withValue:(int)(UIImagePNGRepresentation(sourceImg).length/1000) byType:2];
	}
	if (type == 2) {
		[dfhgwhruebvhrheyavenuetyre addTracking:idcard_cps_size withValue:(int)(imageData.length/1000) byType:2];
		[dfhgwhruebvhrheyavenuetyre addTracking:idcard_size withValue:(int)(UIImagePNGRepresentation(sourceImg).length/1000) byType:2];
	}
	[putTask continueWithSuccessBlock:^id _Nullable(OSSTask * _Nonnull task) {
		task = [client presignPublicURLWithBucketName:BucketName withObjectKey:imageName];
		if(!task.error) {
			if(result) {
				result(YES, imageName, imageData.length);
			}
		}else {
			if(result) {
				result(NO, imageName, imageData.length);
			}
		}
		return nil;
	}];
}
+ (NSData *)compressImage:(UIImage *)source inKiloByte:(NSUInteger)kb {
	CGFloat rate = 1;
	NSData *imageData = UIImageJPEGRepresentation(source, rate);
	while (imageData.length/1204 > kb && rate > 0.3) {
		rate -= 0.05;
		imageData = UIImageJPEGRepresentation(source, rate);
	}
	return imageData;
}
+ (NSString *)generateStringWithBit:(NSInteger)bit {
	NSString *charers = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	NSMutableString *randomString = [NSMutableString stringWithCapacity: bit];
	for (NSInteger i = 0; i < bit; i++) {
		[randomString appendFormat: @"%C", [charers characterAtIndex: arc4random() % charers.length]];
	}
	return randomString;
}
@end
